/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.composite.CompositeGeneratorContext;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.entity.entity.dynamicProp.IDynamicPropSet;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.CefCoreGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;
import java.util.HashMap;
import java.util.Map;

public class EntityGeneratorContext extends CompositeGeneratorContext {

  private HashMap<String, MediateType> childInterfaceTypes;
  private CefCoreGeneratorContext privateProjContext;
  private Class privateMainInterfaceType;
  private DataTypeImpContext privateMainDataTypeContext;
  private Map<String,MediateType> dataImplTypes=new HashMap<>();
  //#region DtmAssemblerTypes
  private HashMap<String, MediateType> privateAfterModifyDtmAssemblerTypes = new HashMap<>();
  private HashMap<String, MediateType> privateAfterCreateDtmAssemblerTypes = new HashMap<>();
  private HashMap<String, MediateType> privateBeforeSaveDtmAssemblerTypes = new HashMap<>();
  private HashMap<String, MediateType> privateAfterModifyValAssemblerTypes = new HashMap<>();
  private HashMap<String, MediateType> privateBeforeSaveValAssemblerTypes = new HashMap<>();
  private HashMap<String, HashMap<String, MediateType>> privateEnrichedUdtAssoTypes = new HashMap<>();
  private HashMap<String, MediateType> privateEnrichedUdtAssoAcsTypes = new HashMap<>();
  private HashMap<String, MediateType> privateEnrichedUdtAssoReadonlyAcsTypes = new HashMap<>();

  public final CefCoreGeneratorContext getProjContext() {
    return privateProjContext;
  }

  public final void setProjContext(CefCoreGeneratorContext value) {
    privateProjContext = value;
  }

  public final Class getMainInterfaceType() {
    return privateMainInterfaceType;
  }

  public final void setMainInterfaceType(Class value) {
    privateMainInterfaceType = value;
  }

  public HashMap<String, MediateType> getChildInterfaceTypes() {
    return (childInterfaceTypes != null) ? childInterfaceTypes
        : (childInterfaceTypes = new HashMap<String, MediateType>());
  }

  public final DataTypeImpContext getMainDataTypeContext() {
    return privateMainDataTypeContext;
  }

  public final void setMainDataTypeContext(DataTypeImpContext value) {
    privateMainDataTypeContext = value;
  }

  public final HashMap<String, MediateType> getAfterModifyDtmAssemblerTypes() {
    return privateAfterModifyDtmAssemblerTypes;
  }

  public final HashMap<String, MediateType> getAfterCreateDtmAssemblerTypes() {
    return privateAfterCreateDtmAssemblerTypes;
  }

  public final HashMap<String, MediateType> getBeforeSaveDtmAssemblerTypes() {
    return privateBeforeSaveDtmAssemblerTypes;
  }

  public final HashMap<String, MediateType> getAfterModifyValAssemblerTypes() {
    return privateAfterModifyValAssemblerTypes;
  }

  public final HashMap<String, MediateType> getBeforeSaveValAssemblerTypes() {
    return privateBeforeSaveValAssemblerTypes;
  }

  public final HashMap<String, HashMap<String, MediateType>> getEnrichedUdtAssoTypes() {
    return privateEnrichedUdtAssoTypes;
  }

  public final HashMap<String, MediateType> getEnrichedUdtAssoAcsTypes() {
    return privateEnrichedUdtAssoAcsTypes;
  }

  public final HashMap<String, MediateType> getEnrichedUdtAssoReadonlyAcsTypes() {
    return privateEnrichedUdtAssoReadonlyAcsTypes;
  }

  //#endregion
  private IGspCommonDataType node;

  public IGspCommonDataType getNode() {
    return node;
  }

  public void setNode(IGspCommonDataType node) {
    this.node = node;
  }

  public TypeInfo getAssoFieldTypeInfo(IGspCommonField field){
    MediateType mediateType = new MediateType(getProjContext().getCefGenContext().getEntityGenContext().getDefaultPackageName(), field.getLabelID() + "");
    TypeInfo typeInfo = new TypeInfo(mediateType.getFullName());
    typeInfo.setUseFullName(true);
    return typeInfo;
  }
  public TypeInfo getFieldTypeInfo(String nodeCode,IGspCommonField field)
  {
    TypeInfo typeInfo=null;
    if(field.getIsUdt())
    {
      if(field.getObjectType() == GspElementObjectType.Association && field.containRefElementNotFromAssoUdt())
      {
        typeInfo = new TypeInfo(
            getProjContext().getCefGenContext().getEntityGenContext().getDataTypeDataGenContexts()
                .get(field.getID()).getEnrichedTypes().get(field.getLabelID()).getFullName());
        typeInfo.setUseFullName(true);
      }
      else
      {
//        UnifiedDataTypeDef unifiedDataTypeDef= (UnifiedDataTypeDef) getProjContext().getCefGenContext().getMetadataJitContext().getMetadata(field.getUdtID()).getContent();
        typeInfo=  Utils.getUdtType(field.getUdtPkgName(),field.getUdtID(),this.getProjContext().getCefGenContext());
        typeInfo.setUseFullName(true);

//        typeInfo=new TypeInfo(unifiedDataTypeDef.getGeneratedEntityClassInfo().getClassFullName());
      }
    }
    else if(field.getObjectType()== GspElementObjectType.Association)
    {
     typeInfo=new TypeInfo( getProjContext().getCefGenContext().getEntityGenContext().getElementAssociationTypes().get(field.getID()).getFullName());
      typeInfo.setUseFullName(true);
    }
    else if(field.getObjectType()==GspElementObjectType.Enum)
    {
      typeInfo=new TypeInfo(getProjContext().getCefGenContext().getEntityGenContext().getElementEnumTypes().get(field.getID()).getFullName());
      typeInfo.setUseFullName(true);
    }
    else if(field.getObjectType()==GspElementObjectType.DynamicProp)
    {
      typeInfo=new TypeInfo(IDynamicPropSet.class);
    }
    else
    {
      typeInfo=Utils.getNativeType(field,getProjContext().getCefGenContext());
    }
    return typeInfo;

  }

  public Map<String, MediateType> getDataImplTypes() {
    return dataImplTypes;
  }
//  //TODO: vo集成cef后去掉
//  public CefDataAttribute getDataAttribute(String nodeCode) {
//    java.lang.Class actionImpType = getProjContext().getCurrentTypeInfo().getEntityEntityTypeInfos()
//        .get(nodeCode).getActionImplType();
//    return (CefDataAttribute) ReflectionUtils.getAnnotation(actionImpType, CefDataAttribute.class);
//  }
}
